<?php

/* Web Statistics and Analytics Console (PHP)
 *  functions.statistics.php
 *  Author: Trevor Bidyk
 *  Created: January 1, 2000
 *  Last modified: April 2, 2011
 *   (C) Us 2011
 *   Uses Simple PHP Framework, licensed under the FreeBSD license.
 *    View license in ./include/LICENSE.
 *   All rights reserved.
 */

// Displays the header that every page displays.
// The included page has logic that will correctly display the correct navigation
// and subnavigation elements.
function DisplayHeader($CurrentPage = null, $CurrentSubPage = null) {
    require_once('./subpages/master-header.php');
}

function DisplayFooter() {
    require_once('./subpages/master-footer.php');
}

// Generate a select with values and display values.
// Set $SelectedIndex to the key of $Values to select a value.
function GenerateSelect($Values, $DisplayedValues, $SelectedIndex = null) {
    $Options = "";
    $Count = sizeof($DisplayedValues);
    if (!is_array($Values) || !is_array($DisplayedValues)) {
        return "<option disabled=\"disabled\">Values provided were not arrays</option>";
    } else if ($Count != sizeof($Values)) {
        return "<option disabled=\"disabled\">Array sizes do not match</option>";
    }
    for ($i = 0; $i < $Count; $i++) {
        if (!is_null($SelectedIndex) && $i == $SelectedIndex) {
            $Options .= "<option value=\"$Values[$i]\" selected=\"selected\">$DisplayedValues[$i]</option>";
        } else {
            $Options .= "<option value=\"$Values[$i]\">$DisplayedValues[$i]</option>";
        }
    }
    return $Options;
}

// Generate a set of month options for a select.
function GenerateMonthSelect($Default = null) {
    $MonthArray = array();
    for ($i = 1; $i < 13; $i++) {
        $MonthArray[] = date("F", mktime(0, 0, 0, $i, 1));
    }
    return GenerateSelect(range(1, 12), $MonthArray, (!is_null($Default) ? ($Default - 1) : (date('n') - 1)));
}

// This function works great for url parameters. It only will allow a value from
// the whitelist. If it cannot find an allowed value, it will provide the default value.
function SanitizeWhiteList($Value, $WhiteList, $DefaultValue = null) {
    $NewValue = array_search($Value, $WhiteList);
    return ($NewValue !== false) ? $WhiteList[$NewValue] : $DefaultValue;
}

// Creates a random string of said length. Using clever string to array technology,
// we do barely any work. Trevor rocks.
function GenerateString($Length) {
    $Characters = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890', 1);
    $String = '';
    for ($i = 0; $i < $Length; $i++) {
        $String .= $Characters[rand(0, sizeof($Characters))];
    }
    return $String;
}

// Creates an IPv4 address. v6 is totally useless in our context.
function GenerateIP() {
    return rand(0, 255) . "." . rand(0, 255) . "." . rand(0, 255) . "." . rand(0, 255);
}

// Returns one of the following fairly common browsers. I realize this list is
// way out of date, but it suites most people on the Internet.
function GenerateUserAgent() {
    $UserAgents = array(
        'Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20110319 Firefox/4.0',
        'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2) Gecko/20100115 Firefox/3.6',
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.0',
        '(Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0))',
        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; MS-RTC LM 8)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)',
        'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1; .NET CLR 1.1.4322)',
        'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9',
        'Opera 9.4 (Windows NT 6.1; U; en)',
        'Opera/9.30 (Nintendo Wii; U; ; 2047-7;en)',
        'Opera/9.20 (Windows NT 6.0; U; en)',
        'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/6.0',
        'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.1 Safari/532.9',
        'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.0 Safari/532.0',
        'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10',
        'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_7; en-us) AppleWebKit/533.4 (KHTML, like Gecko) Version/4.1 Safari/533.4',
        'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16',
        'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.25 (KHTML, like Gecko) Version/3.2 Safari/525.25',
        'Mozilla/5.0 (PLAYSTATION 3; 2.00)',
        'facebookscraper/1.0(+http://www.facebook.com/sharescraper_help.php)',
        'Googlebot/2.1 ( http://www.googlebot.com/bot.html)',
        'Googlebot-Image/1.0 ( http://www.googlebot.com/bot.html)',
        'msnbot/1.1 (+http://search.msn.com/msnbot.htm)');
    return $UserAgents[rand(0, (sizeof($UserAgents) - 1))];
}

// This function generates a date in time. I have made it mostly random.
// Dates are randomly assigned around a range around the current time. This
// creates more "believable" data.
function GenerateDate($DateRange = 3600) {
    return date('Y-m-d H:i:s', time() - rand(0, $DateRange));
}

function FormatDate($Day, $Month, $Year) {
    return date('Y-m-d H:i:s', mktime(0, 0, 0, $Month, $Day, $Year));
}

// Generate a random event type each time.
function GenerateEventType() {
    $EventTypes = explode(',', 'AfterPrint,BeforeUnload,Blur,Change,CheckboxStateChange,Click,ContextMenu,Copy,Cut,DoubleClick,DOMActive,DOMCharacterDataModified,DOMNodeInserted,DOMNodeInsertedIntoDocument,DOMNodeRemoved,DOMSubtreeModified,Drag,DragLeave,DragStart,Drop,Error,Focus,HashChange,Input,KeyDown,KeyPress,Load,Message,MouseDown,MouseOver,MouseUp,PageLeave,PageLoad,MouseWheel,Paste,RadioStateChange,Reset,Resize,OnScroll,Select,SelectStart,Submit,Unload,UserDefined');
    return $EventTypes[rand(0, (sizeof($EventTypes)) - 1)];
}

// Generate random pages and stuff.
function GenerateParameters() {
    $URL = explode(',', '/home,/main,/view,/stat,/catalogue,/?ref=home,/?ref=home#!/profile.php?id=1421229460&sk=photos,/statistics/?hubname,/dc/launch?.gx=1&.rand=d2dbdgvji0ndn,/watch?v=mRUiuPgt3CY&feature=feedu,/gp/product/1593074220');
    return $URL[rand(0, (sizeof($URL)) - 1)];
}

// Generate referrers and whatever.
function GenerateReference() {
    $Referrers = explode(',', 'http://www.google.ca/,http://ca.yahoo.com/,http://www.ebay.com/,http://www.blogger.com/home,http://www.autotrader.com/,http://www.imdb.com/,https://www.paypal.com,http://www.bing.com/,http://ca.msn.com/iat/us_ca.aspx,http://www.facebook.com/?ref=home,http://www.myspace.com/,http://en.wikipedia.org/wiki/Wiki');
    return $Referrers[rand(0, (sizeof($Referrers) - 1))];
}

function GetEvents() {
    $db = Database::getDatabase();
    $Result = $db->query('SELECT Type_ID, Type_Name FROM Event_Type');
    if ($db->hasRows($Result)) {
        $Events = array();
        $EventCount = $db->numRows($Result);
        for ($i = 0; $i < $EventCount; $i++) {
            $Temp = $db->getRow();
            $Events[$Temp['Type_ID']] = $Temp['Type_Name'];
        }
        return $Events;
    } else {
        echo 'No event types were found in the database.';
        exit;
    }
}

?>
